package models

import (
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

func init() {
	//init
}

//gd_id为0则返回所有工单，或根据gd_id返回单个工单,/api/v1/gd/?:id
func (this Sut_gd) Get_gds(gd_id int) (res []Sut_gd) {
	//视图：id | riqi| jc | jsmc | gzxx | clnr | jjsj | gzlx_id | gzlx | gzsb_id | gzsb   | jsr_id | jsr  |
	var (
		sqlstr string
		gd     Sut_gd
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	if gd_id == 0 {
		//取所有工单
		sqlstr = "select * from v_wwfw_gd"
		stmt, err = db.Prepare(sqlstr)
		rows, err = stmt.Query()
	} else {
		//取id号的工单
		sqlstr = "select * from v_wwfw_gd where id=?"
		stmt, err = db.Prepare(sqlstr)
		rows, err = stmt.Query(gd_id)
	}
	for rows.Next() {
		err = rows.Scan(&gd.Id, &gd.Riqi, &gd.Jc, &gd.Jsmc, &gd.Gzxx, &gd.Clfs_id, &gd.Clfs, &gd.Clnr, &gd.Jjsj, &gd.Gzlx_id, &gd.Gzlx, &gd.Gzsb_id, &gd.Gzsb, &gd.Jsr_id, &gd.Jsr)
		checkErr(err)
		res = append(res, gd)
		///log.Println("从数据库读取到工单信息：", gd)
	}
	return res
}

//分页查询，返回分页查询的工单，userid为0则查询所有用户的数据
func (this Sut_gd) Get_fy_gds(pageSize int, pageNumber int, userId int) (res []Sut_gd) {
	//视图：id | riqi| jc | jsmc | gzxx | clnr | jjsj | gzlx_id | gzlx | gzsb_id | gzsb   | jsr_id | jsr  |
	var (
		sqlstr string
		gd     Sut_gd
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	if userId == 0 {
		sqlstr = "select * from v_wwfw_gd limit ?,?" //查询所有用户的数据
		stmt, err = db.Prepare(sqlstr)
		rows, err = stmt.Query((pageNumber-1)*pageSize, pageSize)
	} else {
		sqlstr = "select * from v_wwfw_gd where jsr_id=? limit ?,?" //查询用户ID的数据
		stmt, err = db.Prepare(sqlstr)
		rows, err = stmt.Query(userId, (pageNumber-1)*pageSize, pageSize)
		//log.Println("查询用户的工单数据，用户ID：", userId, "，sql:", sqlstr)
	}
	for rows.Next() {
		err = rows.Scan(&gd.Id, &gd.Riqi, &gd.Jc, &gd.Jsmc, &gd.Gzxx, &gd.Clfs_id, &gd.Clfs, &gd.Clnr, &gd.Jjsj, &gd.Gzlx_id, &gd.Gzlx, &gd.Gzsb_id, &gd.Gzsb, &gd.Jsr_id, &gd.Jsr)
		checkErr(err)
		res = append(res, gd)
		///log.Println("从数据库读取到工单信息：", gd)
	}
	return res
}

//返回用户的BOOTSTRAP分页数据，，使用bootstrapTable的服务器端分页,需要返回的数据有总记录数和行数据
//userId为0则查询所有用户，userId为数字则查询此用户的数据
func (this Sut_gd) Get_fy_gd(pageSize int, pageNumber int, userId int) (res Bootstrap_fy) {
	var ()
	res.Rows = this.Get_fy_gds(pageSize, pageNumber, userId)
	res.Total = this.Get_gd_count(userId)
	return res
}

//取得工单记录总数
func (this Sut_gd) Get_gd_count(userId int) (res int) {
	var (
		sqlstr string
		err    error
	)
	if userId == 0 {
		sqlstr = "select count(*) from wwfw_gd"
		err = db.QueryRow(sqlstr).Scan(&res)
	} else {
		sqlstr = "select count(*) from wwfw_gd where jsr_id=?"
		err = db.QueryRow(sqlstr, userId).Scan(&res)
	}
	if err != nil {
		return -1
	}
	return res
}

//保存工单
func (this Sut_gd) Insert() (err error) {
	//id | riqi | jc | jsmc | gzxx| clnr| jjsj | gzlx_id | gzsb_id | jsr_id |
	var (
		sqlstr string
		//result sql.Result
	)
	if this.Gzsb_id == 0 {
		this.Gzsb_id = 27 //如果传入的故障设备ID号是空或0，则把故障设备改成数据库里“无设备”的设备ID号
	}
	sqlstr = "insert wwfw_gd set riqi=?,jc=?,jsmc=?,gzxx=?,clfs_id=?,clnr=?,jjsj=?,gzlx_id=?,gzsb_id=?,jsr_id=?"
	_, err = db.Exec(sqlstr, this.Riqi, this.Jc, this.Jsmc, this.Gzxx, this.Clfs_id, this.Clnr, this.Jjsj, this.Gzlx_id, this.Gzsb_id, this.Jsr_id)
	checkErr(err)
	return err
}
